﻿#region 程序集引用
using System;
using System.Web;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
#endregion

#region 版权注释
/*===========================================================================
// Copyright (C) 2017 Zhang.cn，并保留所有 
// 作者：吴岸标 QQ:405403610
// 创建日期：2017-09-03
// 功能描述：商品显示处理方法
===========================================================================*/
#endregion

namespace Zhang
{
    namespace Utils
    {
        /// <summary>
        /// 商品显示处理方法
        /// </summary>
        public static class Split
        {
            private static int SplitEllipsisBothNum = 2;
            private static int SplitEllipsisMiddleNum = 5;

            /// <summary>
            /// 获取商品分页数据
            /// </summary>
            /// <param name="reqURL">请求URL</param>
            /// <param name="pgSize">分页大小</param>
            /// <param name="pgIndex">当前请求页码</param>
            /// <param name="totalRS">商品记录总数</param>
            /// <returns></returns>               
            public static Model.Common.Split.SplitData GetSplitData(string reqURL, int pgSize, int pgIndex, int totalRS)
            {
                Model.Common.Split.SplitData model;
                string urlTemplate;

                model = new Model.Common.Split.SplitData();

                model.Total = totalRS;
                model.PageSize = pgSize;
                model.PageCount = totalRS / model.PageSize;

                if (model.PageCount * model.PageSize < totalRS)
                {
                    model.PageCount = model.PageCount + 1;
                }

                model.PageIndex = pgIndex;

                if (reqURL.Contains("&pg=") == true || reqURL.Contains("?pg="))
                {
                    urlTemplate = Regex.Replace(reqURL, @"([?|&])pg=[^&]*", "$1pg={pg}", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                }
                else
                {
                    if (reqURL.Contains("="))
                    {
                        urlTemplate = reqURL + "&pg={pg}";
                    }
                    else
                    {
                        urlTemplate = reqURL + "?pg={pg}";
                    }
                }

                // 匹配失败
                if (urlTemplate == reqURL)
                {
                    urlTemplate = Regex.Replace(reqURL, @"^(.*)(\d+)\.html(.*)$", "$1{pg}.html$3", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                }

                if (totalRS > model.PageCount * model.PageSize)
                {
                    model.PageCount = model.PageCount + 1;
                }

                model.Home = urlTemplate.Replace("{pg}", "1");
                model.Last = urlTemplate.Replace("{pg}", model.PageCount.ToString());

                if (pgIndex > 1)
                {
                    model.Prev = urlTemplate.Replace("{pg}", (pgIndex - 1).ToString());
                }
                else
                {
                    model.Prev = model.Home;
                }

                if (pgIndex < model.PageCount)
                {
                    model.Next = urlTemplate.Replace("{pg}", (pgIndex + 1).ToString());
                }
                else
                {
                    model.Next = model.Last;
                }

                model.URL = GetData(urlTemplate, pgIndex, model.PageCount);
                return model;
            }

            /// <summary>
            /// 获取分页链接数据
            /// </summary>
            /// <param name="urlTemplate">链接模板  {pg} 代表页码</param>
            /// <param name="pgIndex">当前页码</param>
            /// <param name="pgCount">总页数</param>
            /// <returns></returns>
            public static List<Model.Common.Split.SplitLink> GetData(string urlTemplate, int pgIndex, int pgCount)
            {
                List<Model.Common.Split.SplitLink> lstLink = new List<Model.Common.Split.SplitLink>();


                if (pgCount <= SplitEllipsisBothNum * 2 + SplitEllipsisMiddleNum)
                {
                    AppendLink(ref lstLink, urlTemplate, 1, pgCount);
                }
                else
                {
                    if (pgIndex <= SplitEllipsisBothNum + SplitEllipsisMiddleNum / 2)
                    {
                        AppendLink(ref lstLink, urlTemplate, 1, SplitEllipsisBothNum + SplitEllipsisMiddleNum);
                        lstLink.Add(null);
                        AppendLink(ref lstLink, urlTemplate, pgCount - SplitEllipsisBothNum, pgCount);
                    }
                    else
                    {
                        if (pgIndex >= pgCount - SplitEllipsisBothNum - SplitEllipsisMiddleNum / 2)
                        {
                            AppendLink(ref lstLink, urlTemplate, 1, SplitEllipsisBothNum);
                            lstLink.Add(null);
                            AppendLink(ref lstLink, urlTemplate, pgCount - SplitEllipsisBothNum - SplitEllipsisMiddleNum, pgCount);
                        }
                        else
                        {
                            AppendLink(ref lstLink, urlTemplate, 1, SplitEllipsisBothNum);
                            lstLink.Add(null);
                            AppendLink(ref lstLink, urlTemplate, pgIndex - (SplitEllipsisMiddleNum / 2), pgIndex + (SplitEllipsisMiddleNum / 2));
                            lstLink.Add(null);
                            AppendLink(ref lstLink, urlTemplate, pgCount - SplitEllipsisBothNum, pgCount);
                        }
                    }
                }

                return lstLink;
            }

            private static void AppendLink(ref  List<Model.Common.Split.SplitLink> lstLink, string urlTemplate, int startIndex, int endIndex)
            {
                Model.Common.Split.SplitLink link;

                for (int i = startIndex; i <= endIndex; i++)
                {
                    link = new Model.Common.Split.SplitLink();
                    link.Serial = i;
                    link.URL = urlTemplate.Replace("{pg}", i.ToString());
                    lstLink.Add(link);
                }
            }

        }
    }
}